package com.feishu.core.service.factory.in;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.feishu.base.dto.entity.ExecutionTypeDO;
import com.feishu.base.service.AddAttributesDataToInService;
import com.feishu.base.service.ConstructionHttpClientService;
import com.feishu.base.service.FunctionRealizationService;
import com.feishu.base.type.AttributesInterfaceType;
import com.feishu.base.type.ConstructionInterfaceType;
import com.feishu.base.type.FactoryInterfaceType;
import com.feishu.base.type.OutJsonTypeEnum;
import com.feishu.core.service.entity.FeiShuTypeEnum;
import com.feishu.json.utils.ConstructionHttpClientJsonUtils;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.form.service.TyFormDataExtService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@FactoryInterfaceType(defaultValue = "ordinaryMessage")
public class FeiShuEnterParticipateService implements FunctionRealizationService {
    @Resource
    public TyplmPartService typlmPartService;
    @Resource
    private TyFormDataExtService tyFormDataExtService;
    /**
     * 获取输出参数映射
     * @param code
     * @param name
     * @param flowObject
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getInFunctionRealization(ExecutionTypeDO executionType, String code, String name, FlowObject flowObject, Object parameter)
            throws Exception {
        BigInteger oid = flowObject.getRefObjectOid();
        String oType = flowObject.getRefObjectOtype();
        Object partDetails=null;
        if("ty.inteplm.form.CTyFormData".equals(oType)){
            partDetails = this.tyFormDataExtService.getFormDataDetail(oid);
        }
        else if("ty.inteplm.part.CTyPart".equals(oType)){
            partDetails = this.typlmPartService.queryPartDetails(oid, oType);
        }


        JSONObject voJson = JSONObject.parseObject(JSONObject.toJSONString(partDetails));

        AddAttributesDataToInService addAttributesDataToInService=null;
        Map<String,AddAttributesDataToInService> addAttributesDataToInServiceMap=SpringUtil.getBeansOfType(AddAttributesDataToInService.class);

        if(addAttributesDataToInServiceMap!=null){
            for(Map.Entry<String,AddAttributesDataToInService> entity: addAttributesDataToInServiceMap.entrySet()){
                AttributesInterfaceType attributesInterfaceType=entity.getValue().getClass().getAnnotation(AttributesInterfaceType.class);
                if(attributesInterfaceType.typeValue().equals(OutJsonTypeEnum.OUT_JSON_TYPE_FEISHU.value())
                && attributesInterfaceType.classificationValue().equals(FeiShuTypeEnum.OrdinaryMessage.value())){
                    addAttributesDataToInService=entity.getValue();
                    break;
                }
            }
        }
        if(addAttributesDataToInService!=null){
            addAttributesDataToInService.addAttributesToPartDetailsJson(executionType,voJson,partDetails,parameter);
        }

        ConstructionHttpClientService constructionHttpClientService=null;
        Map<String,ConstructionHttpClientService> constructionHttpClientServices=SpringUtil.getBeansOfType(ConstructionHttpClientService.class);
        if(constructionHttpClientServices!=null){
            for(Map.Entry<String,ConstructionHttpClientService> entity: constructionHttpClientServices.entrySet()){
                ConstructionInterfaceType constructionInterfaceType=entity.getValue().getClass().getAnnotation(ConstructionInterfaceType.class);
                if(constructionInterfaceType.defaultValue().equals(OutJsonTypeEnum.OUT_JSON_TYPE_FEISHU.value())){
                    constructionHttpClientService=entity.getValue();
                    break;
                }
            }
        }
        if(constructionHttpClientService!=null){
            List<Map<String, Object>> options = constructionHttpClientService.getOptions(code, name);
            Map<String, Object> inMap = constructionHttpClientService.getInSettingMap(code, name);
            Map<String, Object> bomKeyValue = new HashMap<>();

            String plm_source_type_id=null;
            Map<String, Object> partKey = new HashMap<>();
            for (Map<String, Object> option : options) {
                if ("流程信息".equals(option.get("plmtype"))) {
                    plm_source_type_id= (String) option.get("plm_source_type_id");
                    List<Map<String, Object>> inSetList=(List<Map<String, Object>>) inMap.get(plm_source_type_id);
                    ConstructionHttpClientJsonUtils.setInTree(inSetList, voJson, partKey);
                    break;
                }
            }
            bomKeyValue.putAll(partKey);
            if(plm_source_type_id!=null){
                bomKeyValue.put(plm_source_type_id,partKey);
            }
            return bomKeyValue;
        }

        return null;
    }

    @Override
    public Map<String, Object> getInFunctionRealization(ExecutionTypeDO executionType,String code, String name,
                                                        List<PartDetailsVO> partDetailsVOList) {
        return null;
    }
}
